window.addEventListener("load", () => {
	// 获取验证码&倒计时事件
	$("form #verify button").addEventListener("click", () => {
		let num = 5;
		$("form #verify button").textContent = num;
		$("form #verify button").disabled = true;
		let timer = window.setInterval(() => {
			num--;
			$("form #verify button").textContent = num;
			if (num <= 0) {
				window.clearInterval(timer);
				$("form #verify button").textContent = "重新获取";
				$("form #verify button").disabled = false;
			}
		}, 1000);
	});

	// 创建提示信息标签数组
	const spanArr = [
		$("form #name span"),
		$("form #tel span"),
		$("form #verify span"),
		$("form #pwd span"),
		$("form #agree span"),
	];
	// 失焦事件
	$("form p input").forEach((v, i) => {
		v.addEventListener("blur", () => {
			switch (i) {
				// 判断用户名
				case 0:
					/^[A-Za-z0-9]{6,20}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: (spanArr[i].style.display = "block");
					break;
				// 判断电话号码
				case 1:
					/^1[0-9]{10}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: (spanArr[i].style.display = "block");
					break;
				// 判断验证码
				case 2:
					/^[A-Za-z0-9]+$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: (spanArr[i].style.display = "block");
					break;
				// 判断密码
				case 3:
					/^\w{6,12}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: (spanArr[i].style.display = "block");
					break;
				// 判断协议勾选
				case 4:
					v.checked
						? (spanArr[i].style.display = "none")
						: (spanArr[i].style.display = "block");
					break;
			}
		});
	});
	// 表单提交事件
	$("form").addEventListener("submit", (e) => {
		$("form p input").forEach((v, i) => {
			switch (i) {
				// 判断用户名
				case 0:
					/^[A-Za-z0-9]{6,20}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: ((spanArr[i].style.display = "block"), e.preventDefault());
					break;
				// 判断电话号码
				case 1:
					/^1[0-9]{10}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: ((spanArr[i].style.display = "block"), e.preventDefault());
					break;
				// 判断验证码
				case 2:
					/^[A-Za-z0-9]+$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: ((spanArr[i].style.display = "block"), e.preventDefault());
					break;
				// 判断密码
				case 3:
					/^\w{6,12}$/.test(v.value)
						? (spanArr[i].style.display = "none")
						: ((spanArr[i].style.display = "block"), e.preventDefault());
					break;
				// 判断协议勾选
				case 4:
					v.checked
						? (spanArr[i].style.display = "none")
						: ((spanArr[i].style.display = "block"), e.preventDefault());
					break;
			}
		});
	});
});
